package cn.ex;

import java.util.ArrayList;
import java.util.LinkedList;

public class Ex4 {
    public static void main(String[] args) {
        ta(100000);
        tb(100000);
    }

    static void ta(int n) {
        long s = System.currentTimeMillis();
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(0, "hello" + i);
        }
        long e = System.currentTimeMillis();
        System.out.printf("ArrayList add : %d%n",e-s);

        //删除
        s = System.currentTimeMillis();
        for(int i=0;i<n/2;i++) {
            list.remove(i);
        }
        e = System.currentTimeMillis();
        System.out.printf("ArrayList remove : %d%n",e-s);

        //随机读取
        s = System.currentTimeMillis();
        for(int i=0;i<n/2;i++) {
            String t = list.get(i);
        }
        e = System.currentTimeMillis();
        System.out.printf("ArrayList read : %d%n",e-s);

    }

    static void tb(int n) {
        long s = System.currentTimeMillis();
        LinkedList<String> list = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            list.addFirst("hello" + i);
        }
        long e = System.currentTimeMillis();
        System.out.printf("LinkedList add : %d%n",e-s);

        s = System.currentTimeMillis();
        for(int i=0;i<n/2;i++) {
            list.remove(i);
        }
        e = System.currentTimeMillis();
        System.out.printf("LinkedList remove : %d%n",e-s);

        s = System.currentTimeMillis();
        for(int i=0;i<n/2;i++) {
            String t = list.get(i);
        }
        e = System.currentTimeMillis();
        System.out.printf("LinkedList read : %d%n",e-s);

    }
}
